package com.ipzoe.web.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ipzoe.act.domain.Activity;
import com.ipzoe.cms.domain.Agreement;
import com.ipzoe.cms.domain.Article;
import com.ipzoe.cms.domain.ArticleCategory;
import com.ipzoe.cms.service.IAgreementService;
import com.ipzoe.cms.service.IArticleCategoryService;
import com.ipzoe.cms.service.IArticleService;
import com.ipzoe.system.service.ISysDictDataService;
import com.ipzoe.web.core.page.WebPageInfo;
import com.ipzoe.web.model.ActivityVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/article")
public class ArticleController extends WebBaseController{

    @Autowired
    private IArticleService iArticleService;
    @Autowired
    private IArticleCategoryService iArticleCategoryService;
    @Autowired
    private IAgreementService iAgreementService;

    @RequestMapping("/list/{categoryId}")
    public String list(@PathVariable("categoryId") Long categoryId,
                       @RequestParam(name = "searchKey",required = false) String searchKey,
                       @RequestParam(defaultValue = "1",required = false) int pg, Model model,
                       HttpServletRequest request){

        int pageSize = 20;

        ArticleCategory category = iArticleCategoryService.getById(categoryId);

        LambdaQueryWrapper<Article> articleLqw = new LambdaQueryWrapper<>();
        articleLqw.like(StrUtil.isNotEmpty(searchKey) , Article::getTitle, searchKey);
        articleLqw.eq(Article::getCategoryId, categoryId).orderByAsc(Article::getOrderNum);
        Page<Article> page = iArticleService.page(new Page<>(pg, pageSize), articleLqw);

        model.addAttribute("searchKey", searchKey);
        model.addAttribute("category", category);
        model.addAttribute("articlePage", new WebPageInfo<Article>(page, 8));

        if(2 == categoryId){
            //常见问题
            return getLocaleHtml("qa-list.html", request);
        }else{
            return getLocaleHtml("article-list.html", request);
        }
    }

    @GetMapping("/{id}")
    public String detail(@PathVariable Long id, Model model, HttpServletRequest request){

        Article article = iArticleService.getById(id);
        if(article == null){
            return "error/404.html";
        }

        ArticleCategory category = iArticleCategoryService.getById(article.getCategoryId());

        model.addAttribute("category", category);
        model.addAttribute("article", article);
        return getLocaleHtml("article-detail.html", request);
    }

    @GetMapping("/agreement/{id}")
    public String agreement(@PathVariable Long id, Model model, HttpServletRequest request){

        Agreement agreement = iAgreementService.getById(id);
        if(agreement == null){
            return "error/404.html";
        }

        model.addAttribute("agreement", agreement);
        return getLocaleHtml("agreement-detail.html", request);
    }

}
